<?php

/**
 * Created by JetBrains PhpStorm.
 * User: Grom Hellscream
 * Date: 11/2/12
 * Time: 1:19 PM
 * To change this template use File | Settings | File Templates.
 */
// No direct access to this file
defined('_JEXEC') or die('Restricted access');

// Include dependancy of the main model form
jimport('joomla.application.component.modelform');
// import Joomla modelitem library
jimport('joomla.application.component.modelitem');
// Include dependancy of the dispatcher
jimport('joomla.event.dispatcher');

/**
 * Vien Model
 */
class VastModelVien extends JModelForm {

    /**
     * @var object item
     */
    protected $_item;

    /**
     * Get the vien
     * @return object The vien to be displayed to the user
     */
    function getItem() {

        if (!isset($this->_item)) {
            $id = JRequest::getInt('id');
            
            $db = JFactory::getDBO();
            $query = $db->getQuery(true);

            $query->select('a.*');
            $query->from('#__vien AS a');
            $query->where('a.id = ' . $id);

            $db->setQuery($query);
            $vien = $db->loadObject();
            
            $this->_item = array();
            if (count($vien) > 0) foreach ($vien as $key => $value) {
                $this->_item[$key] = $value;
            }
        }
        
        return $this->_item;
    }
    
    public function updateItem($data) {
        $id = JRequest::getInt('id');
        
        // set the data into a query to update the record
        $db = $this->getDbo();
        $query = $db->getQuery(true);
        $query->clear();
        $query->update('#__vien');
        
        foreach($data as $key => $value) {
            if (is_numeric($value)) {
                $query->set($key . ' = ' . $value);
            } else {
                $query->set($key . ' = ' . $db->quote($value));
            }
        }
        
        $query->where('id = ' . $id);
        
        $db->setQuery($query);
        
        if (!$db->query()) {
            JError::raiseError(500, $db->getErrorMsg());
            return false;
        } else {
            return true;
        }
    }
        
    public function getForm($data = array(), $loadData = true) {
        // Get the form.
        $form = $this->loadForm('com_vast.vien', 'vien', array('control' => 'jform', 'load_data' => true));
        if (empty($form)) {
            return false;
        }
        $jinput = JFactory::getApplication()->input;

        // The front end calls this model and uses a_id to avoid id clashes so we need to check for that first.
        if ($jinput->get('a_id')) {
            $id = $jinput->get('a_id', 0);
        }
        // The back end uses id so we use that the rest of the time and set it to 0 by default.
        else {
            $id = $jinput->get('id', 0);
        }

        return $form;
    }

    protected function loadFormData()
    {
        $data = $this->getItem();
                        
        return $data;
    }
        
}